package com.example.dnfgamercommunitybackend.handler;

import com.alibaba.fastjson.JSONObject;
import com.example.dnfgamercommunitybackend.entity.ResponseResult;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        response.setCharacterEncoding("UTF-8");
        String message = authException.getMessage();
        if (message.equals("账号为封禁状态")){
            response.getWriter().write(JSONObject.toJSONString(ResponseResult.failure(505,message)));
        }else if(message.equals("用户名或密码错误")){
            response.getWriter().write(JSONObject.toJSONString(ResponseResult.failure(501,message)));
        }else {
            response.getWriter().write(JSONObject.toJSONString(ResponseResult.failure(501,"身份认证已过期，请重新登录")));
        }
    }
}
